<?
class CConsultUserOnline
{
	var $LAST_ERROR ="";

	function SetUserOnline ($stream_id, $user_id)
	{
		global $DB;

		$user_id = intval ($user_id);
		$stream_id = intval ($stream_id);

		if ($user_id <= 0 || $stream_id <= 0)
			return;

		$arFields = array(
			"USER_ID"		=> $user_id,
			"STREAM_ID"		=> $stream_id,
			"TIMESTAMP_X"	=> $DB->GetNowFunction(),
		);

		$rows = $DB->Update("b_consult_online", $arFields, "WHERE USER_ID=".$user_id." AND STREAM_ID=".$stream_id);
		if (intval($rows)<=0)
		{
			$DB->Insert("b_consult_online", $arFields);
		}
	}

	function GetUsersOnline ($stream_id, $user_id = false)
	{
		global $DB;

		$user_id = intval ($user_id);
		$stream_id = intval ($stream_id);

		if ($stream_id <= 0)
			return;
		
		$strUser = ($user_id>0) ? " AND USER_ID=".$user_id : "";

		$ONLINE_INTERVAL = 15;

		$strSql = "SELECT CO.USER_ID, CU.NAME, CU.PHOTO
					FROM b_consult_online CO
					LEFT JOIN b_consult_user as CU on CO.USER_ID = CU.ID
					WHERE CO.STREAM_ID=".$stream_id." 
					AND CO.TIMESTAMP_X >= DATE_ADD(now(), INTERVAL - $ONLINE_INTERVAL SECOND)
					".$strUser;
		#var_dump($strSql);
		$dbRes = $DB->Query($strSql, false);
		return $dbRes;
	}
}